;; (start-process "my-process" "*scratch*" "scp" "/srv/lisp/test/o.lisp" "yao@f1:/srv/lisp/test/o.lisp")


(setq nrepl-use-ssh-fallback-for-remote-hosts t)

(defun node-repl () (interactive)
       (setenv "NODE_NO_READLINE" "1") ;avoid fancy terminal codes
       (pop-to-buffer (make-comint "node-repl" "/usr/local/bin/node" nil "--interactive")))

(require 'js-comint)
(setq js-comint-program-command "/usr/local/bin/node")
;; (setq elpy-rpc-virtualenv-path 'current)
(setq python-shell-interpreter "/usr/local/bin/ipython")

;; (setq jedi:environment-virtualenv
;;       (append python-environment-virtualenv
;;               '("--python" "/usr/local/bin/python3")))

(when (memq window-system '(mac ns x))
  (exec-path-from-shell-initialize))

;; (setq jedi:server-command '("/usr/local/bin/python3" "/Users/yao/.emacs.d/.python-environments/default/bin/jediepcserver.py"))
;; (setq jedi:server-command '("/Users/yao/.emacs.d/.python-environments/default/bin/jediepcserver"))


(setq jedi:environment-root "jedi")  ; or any other name you like

(defun my/python-mode-hook ()
  (add-to-list 'company-backends 'company-jedi))

(add-hook 'python-mode-hook 'my/python-mode-hook)

(setq python-indent-offset 4)

(setq elpy-shell-echo-output nil)       ;mac problem ,c-c c-c freez ^G^G^G^G

(setq python-shell-interpreter "ipython"
      python-shell-interpreter-args "-i --simple-prompt")

;; --simple-prompt  输出会越来越多,printt(i) printttt(i)
;; jupyter 只能运行一行代码 for i in range(11): 换行直接出错,只能写成for i in range(11):print(i)


;; (setq python-shell-interpreter "jupyter"
;;       python-shell-interpreter-args "console --simple-prompt" ;;--existing /srv/kernel/kernel-4899.json
;;       python-shell-prompt-detect-failure-warning nil)
;; (add-to-list 'python-shell-completion-native-disabled-interpreters
;;              "jupyter")


;; (add-hook 'python-mode         'elpy-mode)
(elpy-enable)
;; (setenv "IPY_TEST_SIMPLE_PROMPT" "1")

(setq elpy-rpc-backend "jedi") ;; for elpy default
;; (setq-default indent-tabs-mode nil)
;; (setq elpy-shell-use-project-root nil)
;; (define-key fold-mode-menu-bar-map [fold fold-show-all]
;;   '("Show All" . fold-show-all))



(defun run-current-file-new ()
  (interactive)
  (let* (
         (suffixMap
          `(
            ("php" . "php")
            ("pt" . "phpunit")
            ("pl" . "perl")
            ("py" . "python3")
            ("py3" . ,(if (string-equal system-type "windows-nt") "c:/Python32/python.exe" "python3"))
            ("rb" . "ruby")
	    ("rails" . "rails r ")
            ("js" . "node")             ; node.js
            ("sh" . "bash")
            ("exp" . "expect")
            ("ml" . "ocaml")
            ("vbs" . "cscript")
            ;; ("m" . "echo ")
            ("m" . "gcc `gnustep-config --objc-flags` -std=c99 -lgnustep-base -o /tmp/z")
	    ("swift" . "xcrun swift")
            ("dot" . "dot -Tpng ")             ;无用
            )
          )
         (fName (replace-regexp-in-string ".*:" "" (buffer-file-name))) ;去除主机名,可以远程执行
         (fName-no-houzhui (replace-regexp-in-string "\\..*" "" fName)) ;去除后缀的文件名
         (fSuffix (file-name-extension fName)) ;后缀
         (progName (cdr (assoc fSuffix suffixMap))) ;程序名
         (cmdStr (concat progName " \""   fName "\"")) ;命令行
         )
    ;; (when (buffer-modified-p)
    ;;   (when (y-or-n-p "Buffer modified. Do you want to save first?")
    ;;       (save-buffer) ) )
    (when (buffer-modified-p)
      (save-buffer))		    ;自动保存
    (if (string-equal fSuffix "el") ; emacs lisp 源代码
        (load (file-name-sans-extension fName))
      (if progName
          (progn
            (message (concat "Running… " fSuffix progName "@" fName))
            (cond                       ;根据后缀执行命令
             ((string-equal fSuffix "m") (shell-command (concat cmdStr ";/tmp/z"))) ;  objc
             ((string-equal fSuffix "dot") (progn
                                             ;; (message (concat cmdStr " -o " fName-no-houzhui ".png"))
                                             ;; (shell-command (concat cmdStr " -o " fName-no-houzhui ".png"))
                                             (graphviz-dot-preview)
                                             )) ;图片
	     ((and (string-equal fSuffix "py") (string-match "_spider" fName-no-houzhui)) (shell-command (concat "scrapy runspider -L ERROR " fName )))
	     ((and (string-equal fSuffix "py") (string-match "test_" fName-no-houzhui))
              (shell-command (concat "pytest -s " fName ) "*test*")
              (delete-other-windows)
              (split-window-right)
              (other-window 1)
              (switch-to-buffer "*test*")
              (other-window 0)
              )
             ((and (string-equal fSuffix "py"))
              (shell-command (concat "pytest -s "
                                     (replace-regexp-in-string "/routers/\\([^/]*\\)\\." "/tests/test_\\1." fName)
                                     ) "*test*")
              (delete-other-windows)
              (split-window-right)
              (other-window 1)
              (switch-to-buffer "*test*")
              (other-window 0)
              )
             (t (shell-command cmdStr))) ;运行其它程序
            )
        (message "后缀不认识")
        ))
    ))

;; (replace-regexp-in-string "/(.*?)\\." "" "/srv/py/api/xuexiao.py")
;; (global-set-key (kbd "<f5>") 'run-current-file)
;; (define-key elpy-mode-map (kbd "C-c C-c") 'elpy-shell-send-top-statement)
(define-key elpy-mode-map (kbd "C-c C-c") 'run-current-file-new)
